<?php
session_start();
include("load-settings.php");

if(!isset($_SESSION['user']))
	header("Location: login.php");

$user = $_SESSION['user'];

$result = mysql_query("SELECT * FROM user WHERE id = $user");
$row = mysql_fetch_array($result);

if(!isset($_GET['id']))
	header("Location: orders-admin.php");

$order = mysql_real_escape_string($_GET['id']);

if($row['type'] != 2)
	header("Location: home.php");

mysql_query("UPDATE referral_order SET complete = 1 WHERE id = $order");

$order_result = mysql_query("SELECT * FROM referral_order WHERE id = $order");
$order_row = mysql_fetch_array($order_result);
$result = mysql_query("SELECT * FROM user WHERE id = ".$order_row['user']);
$row = mysql_fetch_array($result);
$to = $row['paypal'];
$subject = 'Order complete';
$message = str_replace('\n', '\r\n', $order_complete_message);
$headers = 'From: admin@elocoach.com' . "\r\n" .
	'Reply-To: admin@elocoach.com' . "\r\n" .
	'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

$order_complete_message = mysql_real_escape_string($order_complete_message);
mysql_query("INSERT INTO referral_chat (user, timestamp, message, referral_order) VALUES (4, ".time().", '$order_complete_message', $order)") or die(mysql_error());

header("Location: referral_panel.php");
?>